package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Independent;
import org.light.domain.Statement;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class WebGlobalConfigJavaGenerator extends Independent{
	private static final long serialVersionUID = -9171617426302332622L;

	public WebGlobalConfigJavaGenerator(){
		super();
		this.fileName = "WebGlobalConfig.java";
		this.standardName = "WebGlobalConfig";
	}
	
	public WebGlobalConfigJavaGenerator(String packageToken){
		super();
		this.packageToken = packageToken;
		this.fileName = "WebGlobalConfig.java";
		this.standardName = "WebGlobalConfig";
	}
	
	@Override
	public void setPackageToken(String packageToken){
		this.packageToken = packageToken+"";
	}
	
	@Override
	public String generateImplString() {
		List<Writeable> sList = new ArrayList<Writeable>();
		if (!StringUtil.isBlank(this.packageToken)) {
			sList.add(new Statement(1000L,0,"package "+this.packageToken+";"));
			sList.add(new Statement(1500L,0,""));
		}
		sList.add(new Statement(2000L,0,"import org.springframework.boot.SpringBootConfiguration;"));
		sList.add(new Statement(3000L,0,"import org.springframework.context.annotation.Bean;"));
		sList.add(new Statement(4000L,0,"import org.springframework.web.cors.CorsConfiguration;"));
		sList.add(new Statement(5000L,0,"import org.springframework.web.cors.UrlBasedCorsConfigurationSource;"));
		sList.add(new Statement(6000L,0,"import org.springframework.web.filter.CorsFilter;"));
		sList.add(new Statement(7000L,0,""));
		sList.add(new Statement(8000L,0,"/**"));
		sList.add(new Statement(9000L,0,"* @Author AnHui_XiaoYang"));
		sList.add(new Statement(10000L,0,"* @Email 939209948@qq.com"));
		sList.add(new Statement(11000L,0,"* @Date 2021/5/10 17:07"));
		sList.add(new Statement(12000L,0,"* @Description"));
		sList.add(new Statement(13000L,0,"*/"));
		sList.add(new Statement(14000L,0,"//@SpringBootConfiguration"));
		sList.add(new Statement(15000L,0,"public class WebGlobalConfig {"));
		sList.add(new Statement(16000L,0,""));
		sList.add(new Statement(17000L,1,"@Bean"));
		sList.add(new Statement(18000L,1,"public CorsFilter corsFilter() {"));
		sList.add(new Statement(19000L,0,""));
		sList.add(new Statement(20000L,2,"//创建CorsConfiguration对象后添加配置"));
		sList.add(new Statement(21000L,2,"CorsConfiguration config = new CorsConfiguration();"));
		sList.add(new Statement(22000L,2,"//设置放行哪些原始域"));
		sList.add(new Statement(23000L,2,"config.addAllowedOrigin(\"*\");"));
		sList.add(new Statement(24000L,2,"//放行哪些原始请求头部信息"));
		sList.add(new Statement(25000L,2,"config.addAllowedHeader(\"*\");"));
		sList.add(new Statement(26000L,2,"//暴露哪些头部信息"));
		sList.add(new Statement(27000L,2,"config.addExposedHeader(\"*\");"));
		sList.add(new Statement(28000L,2,"//放行哪些请求方式"));
		sList.add(new Statement(29000L,2,"config.addAllowedMethod(\"GET\");     //get"));
		sList.add(new Statement(30000L,2,"config.addAllowedMethod(\"PUT\");     //put"));
		sList.add(new Statement(31000L,2,"config.addAllowedMethod(\"POST\");    //post"));
		sList.add(new Statement(32000L,2,"config.addAllowedMethod(\"DELETE\");  //delete"));
		sList.add(new Statement(33000L,2,"//corsConfig.addAllowedMethod(\"*\");     //放行全部请求"));
		sList.add(new Statement(34000L,0,""));
		sList.add(new Statement(35000L,2,"//是否发送Cookie"));
		sList.add(new Statement(36000L,2,"config.setAllowCredentials(true);"));
		sList.add(new Statement(37000L,0,""));
		sList.add(new Statement(38000L,2,"//2. 添加映射路径"));
		sList.add(new Statement(39000L,2,"UrlBasedCorsConfigurationSource corsConfigurationSource ="));
		sList.add(new Statement(40000L,4,"new UrlBasedCorsConfigurationSource();"));
		sList.add(new Statement(41000L,2,"corsConfigurationSource.registerCorsConfiguration(\"/**\", config);"));
		sList.add(new Statement(42000L,2,"//返回CorsFilter"));
		sList.add(new Statement(43000L,2,"return new CorsFilter(corsConfigurationSource);"));
		sList.add(new Statement(44000L,1,"}"));
		sList.add(new Statement(45000L,0,"}"));

		return WriteableUtil.merge(sList).getContent();
	}
}
